package io.ticktick.modules.app.controller;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.ticktick.modules.app.annotation.Login;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.ticktick.modules.app.entity.ChatHistoryEntity;
import io.ticktick.modules.app.service.ChatHistoryService;
import io.ticktick.common.utils.R;



/**
 * 用户聊天记录
 *
 * @author hwf
 * @email zhongku0225@gmail.com
 * @date 2019-11-13 17:15:19
 */
@RestController
@RequestMapping("app/chathistory")
public class ChatHistoryController {
    @Autowired
    private ChatHistoryService chatHistoryService;

    /**
     * 列表
     */
    @Login
    @GetMapping("/list/{id}")
    public R list(@PathVariable("id") Long userId, @RequestParam("targetId")Long targetId){
        List<ChatHistoryEntity> list = chatHistoryService.list(
                new QueryWrapper<ChatHistoryEntity>().
                        and(i->i.eq("sender_id", userId).or().eq("sender_id", targetId)).and(i->i.eq("talker_id", userId).or().eq("talker_id", targetId)).orderByAsc("send_time"));

        return R.ok().put("list", list);
    }

    /**
     *
     * @param userId
     * @param status  0 : 获取离线消息
     * @return
     */
    @Login
    @GetMapping("/liststatus/{id}")
    public R infoWithStatus(@PathVariable("id") Long userId, @RequestParam("status")int status){
        List<ChatHistoryEntity> list = chatHistoryService.list(
                new QueryWrapper<ChatHistoryEntity>().eq("talker_id", userId).eq("status", status).orderByAsc("send_time"));

        return R.ok().put("list", list);
    }


    /**
     * 信息
     */
    @Login
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		ChatHistoryEntity chatHistory = chatHistoryService.getById(id);

        return R.ok().put("chatHistory", chatHistory);
    }

    /**
     * 保存
     */
    @Login
    @RequestMapping("/save")
    public R save(@RequestBody ChatHistoryEntity chatHistory){
		chatHistoryService.save(chatHistory);

        return R.ok();
    }

    /**
     * 修改
     */
    @Login
    @RequestMapping("/update")
    public R update(@RequestBody ChatHistoryEntity chatHistory){
		chatHistoryService.updateById(chatHistory);

        return R.ok();
    }

    /**
     * 删除
     */
    @Login
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		chatHistoryService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
